﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows.Forms;

namespace ClassroomAllocationAndManagementFrame.Data
{
    class DataImport
    {
        private string pathForSXD;

        public string PathForSXD
        {
            get
            {
                return pathForSXD;
            }
        }

        public void CampusImport()
        {
            OpenFileDialog ofDialog = new OpenFileDialog();

            ofDialog.InitialDirectory = Application.StartupPath;
            ofDialog.Filter = "All files (*.*)|*.*|sxd files (*.sxd)|*.sxd";
            ofDialog.FilterIndex = 2;
            ofDialog.RestoreDirectory = true;
            if (ofDialog.ShowDialog() == DialogResult.OK)
                pathForSXD = ofDialog.FileName;
        }

        private string pathForGDB;

        public string PathForGDB
        {
            get
            {
                return pathForGDB;
            }
        }

        public void CourseImport()
        {
            //由于在vista中.gdb文件显示为了文件夹，所以只能使用文件夹浏览对话框来获得地址
//             OpenFileDialog ofDialog = new OpenFileDialog();
// 
//             ofDialog.InitialDirectory = Application.StartupPath;
//             ofDialog.Filter = "All files (*.*)|*.*|gdb files (*.gdb)|*.gdb";
//             ofDialog.FilterIndex = 2;
//             ofDialog.RestoreDirectory = true;
//             if (ofDialog.ShowDialog() == DialogResult.OK)
//                 pathForGDB = ofDialog.FileName;

            FolderBrowserDialog dlg = new FolderBrowserDialog();
            dlg.RootFolder = Environment.SpecialFolder.MyComputer;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                pathForGDB = dlg.SelectedPath;
            }

            if(CheckOut(pathForGDB))
            {
                MessageBox.Show("导入成功！");
            }
            else
            {
                MessageBox.Show("请重新导入");
            }

        }

        private bool CheckOut(string _pathForGDB)
        {
            FileGeodatabaseOperator fGDBOperator = FileGeodatabaseOperator.GetInstance();
            try
            {
                if (fGDBOperator.InitializeGDB(pathForGDB) && fGDBOperator.GetTable("tb_classinfo") && fGDBOperator.GetTable("tb_courseinfo")&&
                    fGDBOperator.GetTable("tb_class_course")&&fGDBOperator.GetTable("tb_class_exam")&&fGDBOperator.GetTable("tb_examinfo")&&
                    fGDBOperator.GetTable("tb_manager"))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
        }
    }
}
